#ifndef MQTT_H
#define MQTT_H

#include <QObject>
#include <QtMqtt/QMqttClient>
#include <QMqttMessage>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDateTime>
#include <QDebug>
#include <QTimer>


// 控制命令结构体
struct controllerCmd {
    int farm_id;
    QString status;      // "on" 或 "off"
    QString time_stamp;
};

struct constantValueCmd {
    int farm_id;
    double co2;
    double temperature;
    double humidity;
    double light_intensity;
    QString time_stamp;

};

struct getConstantValue {
    int farm_id;
    QString time_stamp;
};


class mqtt : public QObject
{
    Q_OBJECT
public:
    explicit mqtt(QObject *parent,QString hostname,int id,uint16_t port,double con_co2,double con_tem,double con_hum,double con_light);
    int sendData(int id,double co2,double tem,double hum,double light,QString time);
    int sendOnline(int id,QString status,QString time);
    int sendLimitData(int id,double co2,double tem,double hum,double light,QString time);
    void handleMessage(const QByteArray &message, const QMqttTopicName &topic);
    void processControllerCmd(const QJsonObject &obj);
    void processConstantValueCmd(const QJsonObject &obj);
    void processGetConstantValue(const QJsonObject &obj);



private:
    QString hostname;
    int id;
    uint16_t port;
    QMqttClient *client;
    double con_co2;
    double con_tem;
    double con_hum;
    double con_light;
    controllerCmd  conCmd;
    constantValueCmd contValueCmd;
    getConstantValue getConValue;
    QTimer *reconnectTimer;
    QTimer * keepAliveTimer;

signals:
    // ★ 新增：收到云端“设置阈值”后，把新阈值发给上层（Widget）
    void constantValueReceived(int farm_id,
                               double co2,
                               double temperature,
                               double humidity,
                               double light_intensity,
                               const QString &time_stamp);
};

#endif // MQTT_H
